﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ConceptCorporateAdmin.Gui;
using System.Security;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using ConceptCorporateAdmin.Gui.Dialogs;


namespace ConceptCorporateAdmin
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisibleAttribute(true)]
    public partial class Editor : UserControl
    {
        public static string AppPath
        {
            get
            {
                String ret = new FileInfo(Application.ExecutablePath).Directory.FullName;
                return ret;
            }
        }


        public Editor()
        {
         
            InitializeComponent();

            String file = AppPath + "\\editor.htm";
            if (File.Exists(file))
            {
                Browser.Navigate(file);
                Browser.ObjectForScripting = this;
            }
        
        }

        public string DocumentText
        {
            get
            {
                String ret = "";
                if (Browser.Document != null && Browser.Document.Body != null)
                {
                    ret = Browser.Document.Body.InnerHtml;
                }
                return ret;
            }
            set
            {
                if (Browser.Document != null && Browser.Document.Body != null)
                    Browser.Document.Body.InnerHtml = value;
            }
        }

        public void RightClick()
        {
            MeniuTabel.Show(Cursor.Position);
        }
        

        private void BoldBtn_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("bold", true, true);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("italic", true, true);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("underline", true, true);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("JustifyLeft", true, true);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("JustifyCenter", true, true);
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("JustifyRight", true, true);
        }

        private void toolStripColorPicker1_SelectedColorChanged(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("BackColor", true, ColorToHexString(toolStripColorPicker1.Color));
        }

        #region sys

        string table_code = "<table border='1'><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>";

        static char[] hexDigits = {
         '0', '1', '2', '3', '4', '5', '6', '7',
         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

        public string ColorToHexString(Color color)
        {
            byte[] bytes = new byte[3];
            bytes[0] = color.R;
            bytes[1] = color.G;
            bytes[2] = color.B;
            char[] chars = new char[bytes.Length * 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                int b = bytes[i];
                chars[i * 2] = hexDigits[b >> 4];
                chars[i * 2 + 1] = hexDigits[b & 0xF];
            }
            return new string(chars);
        }

        #endregion

        private void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Browser.Document.Body.SetAttribute("contenteditable", "true");
            NameService.setDocument(Browser.Document);
        }

        private void toolStripColorPicker2_SelectedColorChanged(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("ForeColor", true, ColorToHexString(toolStripColorPicker2.Color));
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("InsertOrderedList", true, true);
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("InsertUnorderedList", true, true);
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("InsertHorizontalRule", true, true);
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            table_code = NewTableDialog.ShowMe();
            if (!table_code.Equals(""))
            {
                string id = NameService.addNextName();
                Browser.Document.ExecCommand("InsertParagraph", true, id);
                Browser.Document.GetElementById(id).InnerHtml = table_code;
                Browser.Document.InvokeScript("refreshEvents");
            }
        }

        private void randDedesubtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addRowBelow");
        }

        private void CmbFont_Click(object sender, EventArgs e)
        {

        }

        private void CmbFont_SelectedIndexChanged(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("FontName", true, CmbFont.Text);
        }

        private void CmbFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("FontSize", true, CmbFontSize.Text);
        }

        private void BtnTable_ButtonClick(object sender, EventArgs e)
        {
            table_code = NewTableDialog.ShowMe();
            if (!table_code.Equals(""))
            {
                string id = NameService.addNextName();
                Browser.Document.ExecCommand("InsertParagraph", true, id);
                Browser.Document.GetElementById(id).InnerHtml = table_code;
                Browser.Document.InvokeScript("refreshEvents");
            }
        }

        private void btnRandDedesubt_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addRowBelow");
        }

        private void btnRandDeasupra_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addRowAbove");
        }

        private void btnStergeRand_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("removeRow");
        }

        private void btnColoanaStanga_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addColumnToLeft");
        }

        private void btnColoanaDreapta_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addColumnToRight");
        }

        private void btnStergeColoana_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("removeColumn");
        }

        private void btnMergeCell_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("mergeCells");
        }

        private void randDedesubtToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addRowBelow");
        }

        private void randDeasupraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addRowAbove");
        }

        private void stergeRandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("removeRow");
        }

        private void coloanaLaStangaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addColumnToLeft");
        }

        private void coloanaLaDreaptaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("addColumnToRight");
        }

        private void stergeColoanaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("removeRow");
        }

        private void maresteCelulaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Browser.Document.InvokeScript("mergeCells");
        }

        private void BtnLinkAct_Click(object sender, EventArgs e)
        {
            int id_act = LinkActNormativ.ShowMe();
            if( id_act != -1 )
                Browser.Document.ExecCommand("CreateLink", false, "javascript:window.external.GotoAct('"+id_act+"');");
        }

        private void BtnRemoveLink_Click(object sender, EventArgs e)
        {
            Browser.Document.ExecCommand("Unlink", false, "");
        }

    }
}
